package com.stacker.management.application;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.util.StreamUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RequestCallback;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;

@RestController
public class AppApplication {
    @Autowired
    RestTemplate stackerRestTemplate;

    @GetMapping("management/apps")
    public void getApp(HttpServletResponse response) {

        response.reset();
        response.setContentType("application/octet-stream");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-Disposition", "attachment; filename=hahha.exe");
        response.setHeader("Cache-Control", "public");


        String url = "https://appdlc-drcn.hispace.hicloud.com/dl/appdl/application/apk/ce/ced0a768618848f39703460e0bcdad22/PC00b677fbea9e41e88c23caa51c6b2444.2107311803.exe";
        RequestCallback requestCallback = request -> request.getHeaders()
                .setAccept(Arrays.asList(MediaType.APPLICATION_OCTET_STREAM, MediaType.ALL));
        stackerRestTemplate.execute(url, HttpMethod.GET, requestCallback, clientHttpResponse -> {
            StreamUtils.copy(clientHttpResponse.getBody(), response.getOutputStream());
            return null;
        });
    }
}
